package com.joshua.viewtest.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import com.joshua.viewtest.R;

/**
 * Created by LONG on 2016/5/20.
 */
public class CustomTextView extends TextView {
    private int textFont;

    public CustomTextView(Context context) {
        this(context, null);
        Log.w("joshua", "CustomTextView: " + 1);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        Log.w("joshua", "CustomTextView: " + 2);

    }

    /**
     * 获得我自定义的样式属性
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.w("joshua", "CustomTextView: " + 3);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable
                        .CustomTextView,
                defStyleAttr, 0);
        int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.CustomTextView_customFont:
                    this.textFont = typedArray.getInt(attr, 0);
                    break;
                default:
                    break;
            }
        }
        String[] typefaceList = context.getResources().getStringArray(R.array.font_list);
        Typeface typeface = Typeface.createFromAsset(context.getAssets(),
                typefaceList[textFont]);
        setTypeface(typeface);
    }
}
